<template>
  <el-dialog title="标题" :visible.sync="showDialog" @close="closeDialog">
    <!-- 内容 -->
    <el-form
      :model="formData"
      :rules="formRules"
      ref="form"
      label-width="120px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="管理员账号" prop="login_name">
            <el-input v-model="formData.login_name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="管理员姓名" prop="name">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="管理员邮箱" prop="email">
            <el-input v-model="formData.email"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="管理员手机" prop="phone">
            <el-input v-model="formData.phone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="管理员密码" prop="password">
            <el-input
              v-model="formData.password"
              type="password"
              show-password
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="管理员姓名">
            <el-switch
              v-model="formData.status"
              :active-value="1"
              :inactive-value="0"
              active-text="启用"
              inactive-text="禁用"
            ></el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="角色" prop="role_id">
            <el-checkbox-group v-model="formData.role_id">
              <el-checkbox
                v-for="item in allRoleList"
                :key="item.id"
                :label="item.id"
              >
                {{ item.name }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="showDialog = false">取消</el-button>
      <el-button type="primary" @click="btnOK">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getAllRoleListApi } from '@/api/role'
import { addAdminApi } from '@/api/admin'
export default {
  name: '',
  data() {
    return {
      showDialog: false,
      formData: {
        email: '',
        login_name: '',
        name: '',
        password: '',
        phone: '',
        role_id: [],
        status: 1
      },
      formRules: {
        email: [{ required: true, message: '请输入', trigger: 'blur' }],
        login_name: [{ required: true, message: '请输入', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        password: [{ required: true, message: '请输入', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入', trigger: 'blur' }],
        role_id: [
          {
            required: true,
            type: 'array',
            min: 1,
            message: '请勾选角色',
            trigger: 'blur'
          }
        ]
      },
      allRoleList: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const res = await getAllRoleListApi()
      this.allRoleList = res
    },
    closeDialog() {
      this.formData = {
        email: '',
        login_name: '',
        name: '',
        password: '',
        phone: '',
        role_id: [],
        status: 1
      }
      this.$refs.form.clearValidate()
    },
    async btnOK() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return console.log(error)
      }
      await addAdminApi(this.formData)
      this.$message.success('添加成功')
      this.$emit('updateList')
      this.showDialog = false
    }
  }
}
</script>
<style scoped></style>
